package com.anjie.powerproject.repository;

import com.anjie.powerproject.entity.Model;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ModelRepository extends JpaRepository<Model, Integer> {

    @Query(nativeQuery = true, value = "select * from model where device_code=?1 and type=?2 and status=1")
    List<Model> getModelByDeviceCodeAndType(String deviceCode, int type);

    @Query(nativeQuery = true, value = "select * from model where device_code=?1 and type=?2 and is_used=1 order by id desc limit 1")
    Model getSelectedModel(String deviceCode, int type);

    @Query(nativeQuery = true, value = "select count(*) from model where model_name=?1")
    int getCountModelByName(String modelName);

    @Query(nativeQuery = true, value = "select id from model where model_name=?1")
    int getModelIdByName(String modelName);

    @Transactional
    @Modifying
    @Query(nativeQuery = true,value="update model set model_name = ?2,device_code= ?3,point_id= ?4,model_version = ?5 where id = ?1")
    int updateModel(Integer id,String modelName,String deviceCode,Integer pointId,String modelVersion);

}
